﻿using FineUI;
using SysManage.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using ZHDJ.Core;
using ZHDJ.Data.Models.Edu;
using ZHDJ.WebPage;

namespace SysManage.Module.Edu
{
    public partial class PartyEduLessonList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyEduCoursewareView";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }
        #region 数据加载

        private void BindData()
        {
            // 权限检查
            CheckPowerWithButton("PartyEduCoursewareAdd", btnNew);
            btnNew.OnClientClick = Window1.GetShowReference("~/app/SysManage/Module/Edu/PartyEduLessonEdit.aspx", "新增");
            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;
            ControlBusiness.ControlBindDataDictionary(ddlCategory, SysDataDictionary.EduThemeCategory, true);
            ControlBusiness.ControlBindDataDictionary(ddlCourseType, SysDataDictionary.EduCourseType, true);
        }

        private void LoadData()
        {
            var ds = PartyEduLesson.GetPartyEduCourseware(Grid1.PageIndex, Grid1.PageSize, 
                ddlCategory.SelectedValue,ddlCourseType.SelectedValue,
                txtTitle.Text);

            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithWindowField("PartyEduCoursewareEdit", Grid1, "editField");
            CheckPowerWithLinkButtonField("PartyEduCoursewareDelete", Grid1, "deleteField");
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            LoadData();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string PartyEduLessonId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                //if (!CheckPower("PartyEduCoursewareDelete"))
                //{
                //    CheckPowerFailWithAlert();
                //    return;
                //}
                var del = DB.PartyEduLessons.FirstOrDefault(t => t.ID == PartyEduLessonId);
                DB.PartyEduLessons.Remove(del);
                DB.SaveChanges();
                LoadData();
            }
        }
        #endregion

        #region 按钮事件
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            //if (!CheckPower("PartyEduCoursewareDelete"))
            //{
            //    CheckPowerFailWithAlert();
            //    return;
            //}

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyEduLessons.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyEduLessons.Remove(u));
            DB.SaveChanges();
            //DB.Users.Delete(u => ids.Contains(u.ID));

            // 重新绑定表格
            LoadData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            LoadData();
        }
        #endregion
    }
}
